package com.app.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.app.config.UserConfig;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.fun.knowledge.bean.Article;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.my.MyArticleInfoWriterActivity;
import com.lepu.app.main.bean.BeanAd;

public class MyNetUtil implements AsyncRequest {

    // sample request
    private static final String REQUEST_TEST = "request_test";

    // 这个类处理回调
    private static final String REQUEST_SHARE = "request_share";
    private static final String REQUEST_ARTICLE_DETAIL = "request_article_detail";
    private static final String REQUEST_GET_AD = "request_get_ad";

    // 外部类处理回调
    public static final String REQUEST_ADD_COLLECT = "request_add_collect";
    public static final String REQUEST_CANCEL_COLLECT = "request_cancel_collect";
    public static final String REQUEST_ADD_HELP = "request_add_help";

    private static MyNetUtil mInstance = null;

    private BaseFragmentActivity mBaseFragmentActivity = null;

    private MyNetUtil() {

    }

    public static MyNetUtil getInstance() {
        if (mInstance == null) {
            mInstance = new MyNetUtil();
        }
        return mInstance;
    }

    // ================================================================================

    /***
     * @param baseFragmentActivity
     * @param ObjectType           1-文章，2-活动，3-抽奖,4-app, 5-视频，6-讲座
     * @param ObjectID
     */
    public void requestShare(BaseFragmentActivity baseFragmentActivity,
                             AsyncRequest asyncRequest, int ObjectType, String ObjectID) {

        if (!LoginUtil.checkIsLogin()) {
            return;
        }

        //如果没有目标id，值为 -1
        if (TextUtils.isEmpty(ObjectID)) {
            ObjectID = "-1";
        }

        mBaseFragmentActivity = baseFragmentActivity;

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();

        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);

            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());

            params.put("ObjectType", ObjectType);
            params.put("ObjectID", ObjectID);
        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post_main(Setting.getShareSuccessUrl(), map, null, asyncRequest,
                REQUEST_SHARE, false);
    }

    /**
     * 获取文章详情
     *
     * @param baseFragmentActivity
     * @param ArticleID
     * @param DisplayType
     * @param ContentType
     * @param ArticleTag
     */
    public void requestArticleDetail(BaseFragmentActivity baseFragmentActivity,
                                     String ArticleID, String DisplayType, String ContentType, String ArticleTag) {

        mBaseFragmentActivity = baseFragmentActivity;

        AppEnterTongJi.clickEvent(76, ArticleID);

        int displayType = UtilityBase.parseInt(DisplayType);
        if (displayType == 3) {
            //专栏文章
            String url = Setting.getShareMyArticleInfoUrl(baseFragmentActivity, ArticleID);

            Intent intent = new Intent(mBaseFragmentActivity,
                    MyArticleInfoWriterActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("TargetID", ArticleID);
            mBaseFragmentActivity.startActivity(intent, true);
        } else {
            //普通文章，系列文章,视频文章
            String url = Setting.getShareMyArticleInfoUrl(baseFragmentActivity, ArticleID);

            Intent intent = new Intent(mBaseFragmentActivity,
                    MyArticleInfoActivity.class);

            //只有系列文章需要
            intent.putExtra("ArticleTag", ArticleTag);

            intent.putExtra("url", url);
            intent.putExtra("TargetID", ArticleID);
            mBaseFragmentActivity.startActivity(intent, true);
        }
    }

    /**
     * 获取作家文章详情
     *
     * @param baseFragmentActivity
     * @param ArticleID
     */
    public void requestArticleDetailWriter(BaseFragmentActivity baseFragmentActivity,
                                           String ArticleID) {

        mBaseFragmentActivity = baseFragmentActivity;

        // 文章跳转
        String url = Setting.getShareMyArticleInfoUrl(baseFragmentActivity, ArticleID);

        Intent intent = new Intent(mBaseFragmentActivity, MyArticleInfoWriterActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("TargetID", ArticleID);

        mBaseFragmentActivity.startActivity(intent, true);
    }

    /**
     *
     * @param baseFragmentActivity
     */
    public void requestToolYqspArticleDetail(BaseFragmentActivity baseFragmentActivity) {

        mBaseFragmentActivity = baseFragmentActivity;

        //AppEnterTongJi.clickEvent(76, ArticleID);

        //孕期食谱文章
        String ArticleID = UserConfig.getConfigString(baseFragmentActivity,Const.CONFIG_APP_TOOL_YQSP_ARTICLE_ID,"-1");
        String url = Setting.getToolYqspMyArticleInfoUrl(baseFragmentActivity, ArticleID);

        Intent intent = new Intent(mBaseFragmentActivity,
                MyArticleInfoActivity.class);

        //只有系列文章需要
        intent.putExtra("ArticleTag", "");

        intent.putExtra("url", url);
        intent.putExtra("TargetID", ArticleID);
        intent.putExtra("tool_yqsp",true);
        mBaseFragmentActivity.startActivity(intent, true);
    }

    /**
     * 获取广告
     *
     */
    public void requestGetAd() {

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();

        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);

            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());

        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post_main(Setting.getAdvertisementListUrl(), map, null, this,
                REQUEST_GET_AD, false,false,true);
    }

    // ================================================================================
    @Override
    public void RequestComplete(Object object, Object data) {
        if (object.equals(REQUEST_TEST)) {
            LogUtilBase.LogD(null, "REQUEST_TEST success");
            mBaseFragmentActivity.hideProgressDialog();
        } else if (object.equals(REQUEST_SHARE)) {
            LogUtilBase.LogD(null, "REQUEST_SHARE success");

            try {
                JSONObject jsonObject = new JSONObject((String) data);
                JSONObject obj1 = (JSONObject) jsonObject.get("Result");
                JSONObject dataObject = (JSONObject) obj1.get("DetailInfo");

                int integral = dataObject.optInt("ChangeCredits");
                LoginUtil.jumpIntegral(mBaseFragmentActivity, integral);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (object.equals(REQUEST_ARTICLE_DETAIL)) {
            LogUtilBase.LogD(null, "REQUEST_ARTICLE_DETAIL success");

            mBaseFragmentActivity.hideProgressDialog();
            // 解析json data
            Article detail = Article.parseSimpleData((String) data);
            if (detail != null) {
//				Intent intent = new Intent(mBaseFragmentActivity,
//						MyArticleInfoActivity.class);
//				intent.putExtra("title", detail.ArticleTitle);
//				intent.putExtra("load_data", detail.ArticleContent);
//				intent.putExtra("TargetID", detail.ArticleID);
//				mBaseFragmentActivity.startActivity(intent, true);

                String url = Setting.getShareMyArticleInfoUrl(mBaseFragmentActivity, detail.ArticleID);

                Intent intent = new Intent(mBaseFragmentActivity,
                        MyArticleInfoActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("TargetID", detail.ArticleID);
                mBaseFragmentActivity.startActivity(intent, true);
            }
        } else if (object.equals(REQUEST_GET_AD)) {
            LogUtilBase.LogD(null, "REQUEST_GET_AD success");

            ArrayList<BeanAd> dataList = BeanAd.parseListInfo((String)data);
            Utils.dowithAdRequest(dataList);
        }
    }

    @Override
    public void RequestError(Object object, int errorId, String data) {
        if (object.equals(REQUEST_TEST)) {
            LogUtilBase.LogD(null, "REQUEST_TEST fail");
            mBaseFragmentActivity.hideProgressDialog();
        } else if (object.equals(REQUEST_SHARE)) {
            LogUtilBase.LogD(null, "REQUEST_SHARE fail");
        } else if (object.equals(REQUEST_ARTICLE_DETAIL)) {
            LogUtilBase.LogD(null, "REQUEST_ARTICLE_DETAIL fail");
            mBaseFragmentActivity.hideProgressDialog();
        } else if (object.equals(REQUEST_GET_AD)) {
            LogUtilBase.LogD(null, "REQUEST_GET_AD fail");
        }
    }

}
